//
//  NetWorkTool.m
//  诚诚直播
//
//  Created by 廖易诚 on 16/7/9.
//  Copyright © 2016年 liaoyicheng. All rights reserved.
//

#import "NetWorkTool.h"
#import "AFNetworking.h"

@interface NetWorkTool ()

@property (nonatomic, strong) AFHTTPSessionManager *manager;

@end



@implementation NetWorkTool

- (AFHTTPSessionManager *)manager {
    
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
        _manager.requestSerializer.timeoutInterval = 5.f;
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
    }
    return _manager;
}


+ (instancetype)shareTool {

    static NetWorkTool *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[NetWorkTool alloc] init];
        
    });
    return instance;
    
}


- (void)getWithUrlString:(NSString *)urlString params:(NSDictionary *)dict success:(success)success failure:(failure)failure {
    [self.manager GET:urlString parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
        //
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 成功
        if (success) {
            success(task,responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //
        if (failure) {
            failure(task,error);
        }
    }];
}

- (void)postWithUrlString:(NSString *)urlString params:(NSDictionary *)dict success:(success)success failure:(failure)failure {
    [self.manager POST:urlString parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
        //
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 成功
        if (success) {
            success(task,responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //
        if (failure) {
            failure(task,error);
        }
    }];
}

@end
